﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using _XNA__RunningDeath.GLO;
using _XNA__RunningDeath.DAO;

namespace _XNA__RunningDeath.GUI
{
    class CGUI_GameOver:CDiaLogBox
    {
        Texture2D _texBackground;
        Texture2D _texBtnRePlay;
        Texture2D _texBtnExit;

        Rectangle _posbtnRePlay;
        Rectangle _pobtnExit;
        Rectangle _pobtnExit2;

        public CGUI_GameOver(ContentManager content, GameTime gameTime, SpriteBatch spriteBatch)
            : base(content, gameTime, spriteBatch)
        {
            this._texBackground = this._content.Load<Texture2D>(@"GUI/bgGameOver");
            this._texBtnRePlay = this._content.Load<Texture2D>(@"GUI/btnReplayGameOver");
            this._texBtnExit = this._content.Load<Texture2D>(@"GUI/btnExitGameOver");

            this._dstoado = this._content.Load<Dictionary<string, Rectangle>>(@"GUI/ztoadoXMLgui");
            _posbtnRePlay = this._dstoado["btnReplayGameOver"];
            _pobtnExit2 = _pobtnExit = this._dstoado["btnExitGameOver"];
            _pobtnExit2.X = this._dstoado["btnExitGameOver"].X-100;
        }

        public override void Update(GameTime gameTime)
        {
            //MouseState chuottruoc =  CGLOBAL.MouseCurrent;   //lưu trạng thái chuột vừa click
            CGLOBAL.MouseCurrent = Mouse.GetState();    // Lấy trạng thái chuột mới

            if (CGLOBAL.MouseCurrent.LeftButton == ButtonState.Pressed )
               // if (chuottruoc.LeftButton == ButtonState.Released)//double click
            {
                CGLOBAL.GUISelectedPrev = CGLOBAL.GUISelected;
                if (_posbtnRePlay.Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))  // Contains = chứa cái gì
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.GUISelected = GUIenum.guiRunningDeath;
                    CGLOBAL.NhanVatBus.SetLife();
                    CGLOBAL.timevibiGameOver = 0;
                    CGLOBAL.NextGUI = true;
                    CGLOBAL.GamePlay = enumGamePlay.pause;
                }
                else if (_pobtnExit.Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))  // Contains = chứa cái gì
                {
                    //CGLOBAL.NextGUI = true;
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.GUISelected = GUIenum.guiMainMenu;
                    CGLOBAL.timevibiGameOver = 0;
                    CGLOBAL.NewGame = enumGame.mNew;
                    SaveScore data = new SaveScore();
                    data.PlayerName = CGLOBAL.PlayerNameCurent;
                    data.Score = CGLOBAL.Score;
                    DateTime date = DateTime.Now;
                    string sdate = "" + date.Day + date.Month + date.Year + date.Hour + date.Minute + date.Second + date.Millisecond;
                    CSaveScore.doSave(sdate+".sav", data);
                    KhoiTaoRecDiem();
                }
            }
        }

        private void KhoiTaoRecDiem()
        {
            CSaveScore.loadArrData();
            CGLOBAL.recGameScore.Clear();
            Rectangle _recGameDataStand = new Rectangle(CThamSo.recDataStand.X + 44, CThamSo.recDataStand.Y - 15, CThamSo.recDataStand.Width, CThamSo.recDataStand.Height);
            for (int i = 0; i < CGLOBAL.arrScoreGame.Count; i++)
            {
                if (i == 0)
                {
                    Rectangle recdata = new Rectangle(_recGameDataStand.X, _recGameDataStand.Y, _recGameDataStand.Width, _recGameDataStand.Height);
                    CGLOBAL.recGameScore.Add(recdata);
                }
                else
                {
                    _recGameDataStand.Y = _recGameDataStand.Y + 27;
                    Rectangle recdata = new Rectangle(_recGameDataStand.X, _recGameDataStand.Y, _recGameDataStand.Width, _recGameDataStand.Height);
                    CGLOBAL.recGameScore.Add(recdata);
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (CGLOBAL.Life > 0)
            {
                CGLOBAL.NewGame = enumGame.mOle;
                this._spriteBatch.Draw(this._texBackground, new Vector2(this._dstoado["bgGameOver"].X, this._dstoado["bgPause"].Y), Color.White);
                this._spriteBatch.Draw(this._texBtnRePlay, new Vector2(_posbtnRePlay.X, _posbtnRePlay.Y), Color.White);
                this._spriteBatch.Draw(this._texBtnExit, new Vector2(_pobtnExit.X, _pobtnExit.Y), Color.White);
            }else
            {
                CGLOBAL.NewGame = enumGame.mNew;
                _pobtnExit = _pobtnExit2;
                _posbtnRePlay.X = 0;
                this._spriteBatch.Draw(this._texBackground, new Vector2(this._dstoado["bgGameOver"].X, this._dstoado["bgPause"].Y), Color.White);
                this._spriteBatch.Draw(this._texBtnExit, new Vector2(_pobtnExit2.X, _pobtnExit2.Y), Color.White);
            }
        }
    }
}
